import threading
import requests
import datetime
from apscheduler.schedulers.blocking import BlockingScheduler
import PySimpleGUI as sg
import function
import random, os, json, pathlib, time
from threading import Thread

window = None
DELAY = 0
ACC1, ACC2, ACC3 = "", "", ""
T1, T2, T3 = "", "", ""
def init():
    global ACC1, ACC2, ACC3, T1, T2, T3
    config_path = "BNSwitcher_config.json"
    # print(config_path)
    if pathlib.Path(config_path).exists():
        # print("1")
        info_data = function.read_json(config_path)
        ACC1 = info_data["account1"]
        ACC2 = info_data["account2"]
        ACC3 = info_data["account3"]
        T1 = info_data["t1"]
        T2 = info_data["t2"]
        T3 = info_data["t3"]

def main():
    global window, DELAY
    init()
    layout = [[sg.Text("配置文件:"), sg.Input(r"C:\Users\Administrator\AppData\Roaming\Battle.net\Battle.net.config", key='-f_dir-', size=(50, 1))],
            [sg.Text("账号1:"), sg.Input(ACC1, key='-account1-', size=(30, 1)), sg.Text("时间:"), sg.Input(T1, key='-time1-', size=(10, 1))],
            [sg.Text("账号2:"), sg.Input(ACC2, key='-account2-', size=(30, 1)), sg.Text("时间:"), sg.Input(T2, key='-time2-', size=(10, 1))],
            [sg.Text("账号3:"), sg.Input(ACC3, key='-account3-', size=(30, 1)), sg.Text("时间:"), sg.Input(T3, key='-time3-', size=(10, 1))],
            [sg.Text("延时（秒）:"), sg.Input("300", key='-delay-', size=(3, 1))],
            [sg.Button('开始'), sg.Button('Quit')],
            [sg.Text("要添加的账号:"), sg.Input(key='-newaccount-', size=(30, 1)), sg.Button('添加账号')],
            [sg.Multiline('', size=(45,10), key='-OUTPUT-', autoscroll=True)],]

    window = sg.Window('炉石传说换号助手', layout)
    while True:
        event, values = window.read()
        if event == sg.WINDOW_CLOSED or event == 'Quit':
            break
        if event == '开始':
            accs = FormatAccount(values['-account1-'], values['-account2-'], values['-account3-'], values['-time1-'], values['-time2-'], values['-time3-'])
            f_dir = values['-f_dir-']
            DELAY = int(values['-delay-'])
            window['-OUTPUT-'].print(accs)
            window['-OUTPUT-'].print("开始定时换号")
            window['-OUTPUT-'].print(f_dir)
            p = Thread(target=change_account_thread, args=(accs, f_dir))
            p.start()
        if event == "添加账号":
            new_acc = values['-newaccount-']
            window['-OUTPUT-'].print(values['-newaccount-'])
            function.add_account(f_dir=f_dir, account=new_acc)
    window.close()

def get_account(accs):
    h = datetime.datetime.now().hour
    for i in accs:
        if i[1] <= h and h < i[2]:
            return i[0]
    
def change_account_thread(accs, f_dir):
    global window, DELAY
    while True:
        acc = get_account(accs)
        window['-OUTPUT-'].print(acc)
        function.change_account(f_dir=f_dir, account=acc)
        time.sleep(DELAY)

def trans_time(t: str):
    t = t.split()
    return int(t[0]), int(t[1])

def FormatAccount(a1, a2, a3, t1, t2, t3):
    account = []
    if (t1 != ""):
        account.append([a1, trans_time(t1)[0], trans_time(t1)[1]])
    if (t2 != ""):
        account.append([a2, trans_time(t2)[0], trans_time(t2)[1]])
    if (t3 != ""):
        account.append([a3, trans_time(t3)[0], trans_time(t3)[1]])
    return account

if __name__ == '__main__':
    main()
    
